#include "ac_sampler.h"


// 每周期采样点数(50Hz对应20点，1ms采样一次)
#define SAMPLES_PER_CYCLE 20

// 采样器状态结构体
typedef struct {
    const ACSamplerConfig* config;  // 配置参数
    uint16_t voltage_samples_mv[SAMPLES_PER_CYCLE];  // 电压采样缓存
    uint16_t current_samples_mv[SAMPLES_PER_CYCLE];   // 电流采样缓存
    uint8_t sample_count;           // 当前采样计数
    bool cycle_complete;            // 一周期采样完成标志
    
    // 计算结果
    struct {
        uint32_t voltage_rms_mv;    // 电压有效值(mV×10)
        uint32_t voltage_peak_mv;   // 电压峰值(mV×10)
        uint32_t current_rms_ma;    // 电流有效值(mA×100)
        uint32_t current_peak_ma;   // 电流峰值(mA×100)
        bool valid;                 // 结果有效标志
    } result;
} ACSamplerState;

// 静态全局变量(无malloc)
static ACSamplerState s_sampler;

// 整数开方函数(返回sqrt(x)的整数部分)
static uint32_t int_sqrt(uint32_t x) {
    if (x == 0) return 0;
    uint32_t low = 1, high = x;
    while (low <= high) {
        uint32_t mid = low + (high - low) / 2;
        uint32_t mid_sq = mid * mid;
        
        if (mid_sq == x) return mid;
        if (mid_sq < x) {
            low = mid + 1;
        } else {
            high = mid - 1;
        }
    }
    return high;
}

// 初始化采样器
void ac_sampler_init(const ACSamplerConfig* config) {
    if (config != NULL) {
        s_sampler.config = config;
    }
    ac_sampler_reset();
}

// 重置采样器状态
void ac_sampler_reset() {
    s_sampler.sample_count = 0;
    s_sampler.cycle_complete = false;
    s_sampler.result.valid = false;
}

// 处理1ms中断中的采样数据
void ac_sampler_process_interrupt(uint16_t voltage_mv, uint16_t current_mv) {
    if (s_sampler.sample_count < SAMPLES_PER_CYCLE) {
        // 存储采样值
        s_sampler.voltage_samples_mv[s_sampler.sample_count] = voltage_mv;
        s_sampler.current_samples_mv[s_sampler.sample_count] = current_mv;
        s_sampler.sample_count++;
        
        // 采样完成一周期，进行计算
        if (s_sampler.sample_count >= SAMPLES_PER_CYCLE) {

            s_sampler.cycle_complete = true;
        }
    }
}

// 计算电压电流有效值和峰值
void ac_sampler_calculate() {
    if (s_sampler.config == NULL) return;

    const ACSamplerConfig* cfg = s_sampler.config;
    int32_t v_peak = 0, c_peak = 0;               // 峰值(带符号)
    
    // 处理每个采样点
    for (uint8_t i = 0; i < SAMPLES_PER_CYCLE; i++) 
    {
        // 1. 电压计算
        // ADC值转换为mV(减去偏置)
        int32_t v_mv = (int32_t)( s_sampler.voltage_samples_mv[i] - cfg->bias_mv);
        if (v_mv < 0) v_mv = -v_mv;
        if (v_mv > v_peak) v_peak = v_mv;
        // 2. 电流计算
        int32_t c_mv = (int32_t)(s_sampler.current_samples_mv[i]  - cfg->bias_mv);
      
        if (c_mv < 0) c_mv = -c_mv;
        if (c_mv > c_peak) c_peak = c_mv;
    }
    
    // 电压峰值(mV×10) = 峰值 × 电压比 × 10 / 100
    s_sampler.result.voltage_peak_mv = (v_peak * cfg->voltage_ratio)/ 100;
    // 电流峰值(mA×100)
    s_sampler.result.current_peak_ma = (c_peak * cfg->current_ratio)/ 100;

    s_sampler.result.current_rms_ma =s_sampler.result.voltage_peak_mv*1000/1414 ;
    s_sampler.result.voltage_rms_mv =s_sampler.result.current_peak_ma*1000/1414;
    s_sampler.result.valid = true;
}

// 获取采样结果
bool ac_sampler_get_result(ACSampleResult* result) {
    if (result != NULL && s_sampler.cycle_complete && s_sampler.result.valid) {
        result->voltage_rms_mv = s_sampler.result.voltage_rms_mv;
        result->voltage_peak_mv = s_sampler.result.voltage_peak_mv;
        result->current_rms_ma = s_sampler.result.current_rms_ma;
        result->current_peak_ma = s_sampler.result.current_peak_ma;
        result->sample_count = SAMPLES_PER_CYCLE;
        result->valid = true;
        return true;
    }
    return false;
}

// 检查是否采样完成
bool ac_sampler_is_complete() {
    return s_sampler.cycle_complete;
}
